package com.ltu.controller;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltu.model.request.account.CallDialReq;
import com.ltu.model.request.base.BaseFilterPageReq;
import com.ltu.model.request.base.BaseIdPageReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.request.base.PageReqData;
import com.ltu.model.response.CodeResp;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.rest.ippbx.IppbxRestService;
import com.ltu.service.AccountService;
import com.ltu.service.customize.CompanyService;
import com.ltu.service.impl.BaseServiceImpl;
import com.ltu.config.shiro.ShiroUtil;
import com.ltu.config.shiro.dto.UserDto;
import com.ltu.constant.AccountType;
import com.ltu.constant.CommonConstant;
import com.ltu.domain.mp_entity.AccountEntity;
import com.ltu.domain.mp_entity.customize.CompanyEntity;
import com.ltu.mapper.AccountMapper;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 若尘
 * @since 2021-10-26
 */
@RequiresRoles(value={AccountType.ACCOUNT_TYPE_SYS_STAFF})
@RestController
@RequestMapping("/AccountEntity")
@Api(tags =  "管理员模块")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountController extends BaseServiceImpl<AccountMapper, AccountEntity>{
    private final AccountService accountService;
    private final CompanyService companyService;

   
    @ApiOperation(value="获取分页列表")
    @RequestMapping(value="/getMePage", method= RequestMethod.POST)
    public CodeDataResp<Page<AccountEntity>> getMePage(@RequestBody  BaseFilterPageReq<AccountEntity> req ) {
    	if(req.getEntityParam() == null)
    		req.setEntityParam( new AccountEntity());
    	UserDto  user = ShiroUtil.getPrincipalUser();
    	return super.getMePage(req);
    }
    
    @ApiOperation(value="获取列表")
    @RequestMapping(value="/getList", method= RequestMethod.POST)
    public CodeDataResp<List<AccountEntity>> getList(@RequestBody  BaseFilterPageReq<AccountEntity> req ) {
    	if(req.getPageReq()==null)
    		req.setPageReq(new PageReqData());
    	if(req.getEntityParam() == null)
    		req.setEntityParam( new AccountEntity());
    	UserDto  user = ShiroUtil.getPrincipalUser();
        return super.getMeList(req);
        
    }

    @ApiOperation(value="新增/保存")
    @RequestMapping(value="/edit", method= RequestMethod.POST)
    public CodeDataResp edit(@RequestBody AccountEntity req) {
    	if(StringUtils.isBlank( req.getId())) {
    		QueryWrapper<AccountEntity> wrapper=new QueryWrapper<AccountEntity>();
    		wrapper.lambda().eq(AccountEntity :: getUserName,  req.getUserName()).last("limit 1");
    		AccountEntity a= this.getOne(wrapper);
    		if(a!=null)
    			return CodeDataResp.valueOfFailed("账号名规则不正确，请修改账号名");
    	}else {
    		AccountEntity a= this.getById(req.getId());
    		if(!a.getUserName().equals(req.getUserName()))
    			return CodeDataResp.valueOfFailed("账号名不对，不能改账号名称");
    	}
        return super.edit(req);
    }
    
	@Autowired
	private IppbxRestService ippbxRestService;
	
 	@ApiOperation(value="呼叫、拨号")
    @RequestMapping(value="/callDial", method= RequestMethod.POST)
	public CodeDataResp	callDial(@RequestBody CallDialReq req ) {
 		AccountEntity  account =  this.getById(ShiroUtil.getPrincipalUserId());
		ippbxRestService.callDial(account.getSrcPhone(),  req.getDestPhone());
		return CodeDataResp.valueOfSuccessEmptyData();
	}
	
    
 	@ApiOperation(value="获取详情")
    @RequestMapping(value="/getDetail", method= RequestMethod.GET)
    public CodeDataResp getDetail(@RequestParam String id ) {
 		 BaseIdReq baseIdReq = new BaseIdReq();
 		 baseIdReq.setId(id);
        return super.getDetail(baseIdReq);
    }

 	
    @ApiOperation(value="删除")
    @RequestMapping(value="/remove/{id}", method= RequestMethod.GET)
    public CodeDataResp remove(@PathVariable String id) {
    	BaseIdReq req=new BaseIdReq(id);
    	return super.remove(req);
    }
    
	
}

